package com.huiminpay.transaction.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.huiminpay.common.cache.util.QRCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class PayTestController {
    // 支付宝网关地址
    String serverUrl ="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    //应用id
    String appId="9021000142636691";
    //应用私钥
    String privateKey ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC4NQeuhx/QH5LSQ42ycf+gn+BNPlxUI1gsJTYNZ/UPthAmw/5OI6VpHtVZLolocKgt4IaeiGFuOZXm/H0YjUPVxrKIAw9nO7k3Pi61p8HmO5uFWyT44y7QRLKlYiPG+paR/GctOa+oU5+mGGlgR8o39D+xE8Jw1m0hEzH+DOog2HJZn+25DyF2Kh1e9Pydho9hieRGafc2bqpTP17NzSD6PAAr6WRf8xqfqDV42tVpremz909jMNBWacr8vXqRdZ4Njx17EL2LuxbTs1UM0MLkf468TsLFIKHBiep1k7Rpo9O2k5t4lxrCcQww3lU1iZF0VFuK49VtH8X7i0bz5M0rAgMBAAECggEBAJi6z02zl3r2UsOnYxrrTimdpVomNGNGktq3V6Ox6AxIxgd+RaIACCuqdvZkkWVY0viGytcy+KVtdY55Fjd/GRlsj6YZ83GCoUTed9gB6dC852ugcH0z0CK6cuyQ/F0EdEdCYUGsLKqB+0K5xtUfcNkH2K83VOY9w2nDApZDmvPzV4qyfopA5fhG1LITYyagguaYz7aKWoSWhA3eerO2qPDK5Rr99yHrZInIEbwXXQmNgIkviwkfIbvGR6NdHed076UD4zTaFx9YHzbbcQztaZTRAxv/NA8CQZ+lYLXX4FshXdAftq4ejz5UTgV3hsvtDG8t1sg/5xozXurc9m/iXgECgYEA9onb5/xI617SZj92kCFEayhfb4N3LoBnKkXZDG0kMvkn61SuhTiyOKew/nJwmf7yWDfEHzW+Us2ZJcdcy2YlAQsh19moeynj0ZBEt7qa88FvytOFvFS1WodjZ3Dc+ZjbR4L9HSfLaq5Dp+wwGnZ8g+tJTDjc4g1MmSTvbxR3NKsCgYEAv0bKZUcvHtJM2LqUFJBeGayfAtF31/iYK5i8tsrThC7Cb5bjWHC6R7KmoGVqueZ507tGGR4Wd1bupjy7q+DHaDS8XQq8PgDeOMO5znoCN/FAXezAKvDf414v0c27ECtVv7US+tDhG7BlJwN/cgyvCI8lz33xNON0fhjNaxhryYECgYEAryOeWj5KGxMgisyY0KkhbrKeoZyWJS3InCpEl+RM6/0+U5qz5CYzzbWrMgdgYLXRucPcCoPmcfJm0oS/MiudLBGZeDrCysQmeLBxcMga3gFUBpWfkvvmXaqCfnCaOqJRXIr8fyLPCjlEDN3oClST/i8+wew/VkM2YDDx+0phqMMCgYAIVSYmCVbVKeYtHtuqlatzxOBceBGXd/3xEENQVz+lp3/S6qjWxnpm+zJlFGX02UR/Y84m8/eYlVHqLgUi9LTbJkj6aKhQryr/IA+tHgWQuFLMhK7OcBXgys6cfjDjAHBI3LOJ96WG1kVh0LYxmrnZTpsgEeV7H1mI0L3KJ5MygQKBgAO80IQjAyCAOEqJMp3Bk6T7y5IvIwlsoRl8UeDEovCk4UwzGvZ7KGljx3z5qIE/7WaBzwQWWuORGVZc5/yHKhbzYsTo4kYeAUVwjcFFY1or9+P79Uzy6a9YrciPNXReduFoxE1jsflFCGoAF+goOFqcA2RGv6iZ2z5dzjKbs0/V";

    // 格式化方式
    String format="json";
    // 编码方式
    String  charset = "utf-8";
    // 支付宝公钥
    String alipayPublicKey ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvjnAi9bRyhpYfjCtL7aRQlzARtUGc73DMlHCApe6fa75X2tD5QfKy/qvAqXh0rpSdruwiZO9j2SG5mquq0FO8caGYmVrSJeji14YY04OWGWuEu+iWxB1IsCD0E7ZK2J++YDsuD9DYmxK4/axaqrsRN4FK7GsjMDY4k77GzSa4QkNoHLwZxBzflXaCZ+bWqFBInqp2CuJ1uMiLSkgdvSKFaGhvZiMv0fSqlT+/52184fV4DaeDgPCoJyMdIXtISFVfojpmSWDCB0172SeWIw32oHGUtwsgTq8AIOijyJgNWdNffcGHEqGvPxZ03eQeSNa99o04DEBPkIne8uFeY+GnwIDAQAB";
    // 签名类型
    String signType = "RSA2";
    @GetMapping("/alipaytest")
    public void alipayTest(HttpServletResponse response) {
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(serverUrl,
                appId, privateKey, format, charset, alipayPublicKey,signType);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl("");
        request.setNotifyUrl("");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_trade_no", "20210817010101004");
        jsonObject.put("subject", "iPhone 15 Pro Max 1T基佬紫");
        jsonObject.put("total_amount", 10000);
        jsonObject.put("product_code", "QUICK_WAP_WAY");
        request.setBizContent(jsonObject.toJSONString());
        AlipayTradeWapPayResponse payResponse=null;


            try {
                payResponse = alipayClient.pageExecute(request);
                String body = payResponse.getBody();
                response.setContentType("text/html;charset="+charset);
                response.getWriter().write(format);
                response.getWriter().flush();
                response.getWriter().close();
            } catch (AlipayApiException|IOException e) {
                throw new RuntimeException(e);
            }
    }



}
